<?php
namespace Deployer;

require 'recipe/composer.php';
// Config
set('application','YjzApi');
set('php_container','php83'); //php 容器名称
set('http_user', 'admin');
set('repository', 'git@gitee.com:threemore/yjz-api.sixtymore.cn.git');

add('shared_files', []);
add('shared_dirs', []);
add('writable_dirs', []);
//保留的发布版本数
set('keep_releases', 3);
// Hosts
# 共享文件
add('shared_files', [
    '.env',
    'runtime/hyperf.pid'
]);
# 共享目录
add('shared_dirs', [
    'runtime/logs',
    'runtime/images',
    'storage'
]);
# 可写目录
add('writable_dirs', [
    'runtime',
    'storage'
]);
host('dev-pi')
    ->set('remote_user', 'admin')
    ->set('port', '22')
    ->set('branch', 'dev')  // 指定 dev 分支
    ->set('hostname','192.168.3.35')
    ->set('vendor_path', '/var/www/html/yjz-tst.sixtymore.cn')
    ->set('deploy_path', '/home/admin/docker/www/yjz-tst.sixtymore.cn')
    ->setLabels(['env' => 'dev' ]);

host('online')
    ->set('remote_user', 'root')
    ->set('port', '22')
    ->set('branch', 'master')  // 指定 master 分支
    ->set('hostname','47.100.251.51')
    ->set('vendor_path', '/var/www/html/yjz-api.sixtymore.cn')
    ->set('deploy_path', '/home/docker/php8/yjz-api.sixtymore.cn')
    ->setLabels(['env' => 'prod' ]);


// 自定义任务：安装 Composer 依赖
task('deploy:vendors', function () {

    $releasePath = get('release_path');
    writeln('<info>Installing Composer dependencies...</info>'.$releasePath );
    $vendorPath = explode('/', $releasePath);
    $vendorPathArr = array_splice($vendorPath, -2);
    $vendorPath = get('vendor_path') .'/'. implode('/', $vendorPathArr);
    // composer update
    run("sudo /usr/bin/docker exec {{php_container}} composer update -d $vendorPath");

});


// 重启supervisor 服务
task('supervisor:restart',function () {
    $env = get('labels')['env'] ?? null;
    writeln("<info>发布环境：{$env}</info>");
    $service = $env === 'prod'
        ? 'yjz-api.sixtymore.cn'
        : 'yjz-tst.sixtymore.cn';
    run("sudo /usr/bin/docker exec -i {{php_container}} supervisorctl restart {$service}");
});

after('deploy:failed', 'deploy:unlock');
//after('deploy:vendors','deploy:docs');
after('deploy:success', 'supervisor:restart');
